<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>济宁消防人员人员出入管理系统</title>
<link rel="stylesheet" href="themes/default/easyui.css" />
<link rel="stylesheet" href="themes/icon.css" />
<link rel="stylesheet" href="css/main.css" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.easyui.min.js"></script>
</head>
<body class="easyui-layout">
	<div id="content" data-options="region:'center'">
		<table id="user_dg">
		</table>
	
		<div id="user_toolbar">
			<a href="javascript:addUser();" class="easyui-linkbutton"
				data-options="iconCls:'icon-add',plain:true">新增</a>
		</div>
		<div id="user_menu" class="easyui-menu"
			data-options="onClick:userMenuHandler" style="width: 120px;">
			<div data-options="name:'add',iconCls:'icon-add'">新增</div>
			<div data-options="name:'resetPassword',iconCls:'icon-edit'">重置管理员密码</div>
			<div data-options="name:'remove',iconCls:'icon-remove'">删除</div>
		</div>

		<div id="user_win" style="padding: 15px 30px; text-align: right;"></div>

		<script type="text/javascript">
			var user;
			function init() {
				$('#user_dg').datagrid({
					url : 'user/list',
					method : 'get',
					idField : 'id',
					loadMsg : '数据正在加载中。。。',
					pagination : true,
					pageSize:30,
					fit : true,
					fitColumns : true,
					rownumbers : true,
					columns : [ [ {
						field : 'id',
						hidden : true
					}, {
						field : 'username',
						title : '用户名',
						width : 1
					}, {
						field : 'realname',
						title : '真实姓名',
						width : 2,
						align : 'center'
					}, {
						field : 'phone',
						title : '手机号码',
						width : 1
					}, {
						field : 'organization',
						title : '所属机构',
						width : 2,
						align : 'center'
					}] ],
					toolbar : '#user_toolbar',
					
					onRowContextMenu : function(e, rowIndex, rowData) {
						user = rowData;
						e.preventDefault();
						$("#user_menu").menu('show', {
							left : e.pageX,
							top : e.pageY
						});
					},
					onDblClickRow : function(rowIndex, rowData) {
						user = rowData;
						editUser(rowData.id);
					}
				}).datagrid('getPager').pagination({
					pageSize : 30,
					beforePageText : "第",
					afterPageText : "条",
					displayMsg : "第 {from} 到 {to} 条，共 {total} 条"
				});

			};
			
			function userMenuHandler(item) {
				switch (item.name) {
				case 'add':
					addUser();
					break;
				case 'remove':
					removeUser(user.id)
					break;
				case 'resetPassword':
					resetPassword(user.id);
					break;
				}
			}

			function addUser() {
				$('#user_win').window({
					title : '新增管理员',
					width : 400,
					height : 300,
					modal : true,
					collapsible : false,
					minimizable : false,
					maximizable : false,
					resizable : false,
					href : 'user/add'
				});

			}
			function resetPassword(userId) {

				$.messager.confirm('确认', '此操作会将密码重置为123456,确认要重置密码吗？', function(r) {
					if (r) {
						$.post('user/resetPassword/' + userId,
								function(data) {
									if (data.success) {
										showMsg("密码重置成功。");
									} else {
										$.messager.alert('错误提示', data.message,
												'error');
									}
								});
					}
				});
				
			}
			function removeUser(userId) {
				$.messager.confirm('确认', '您确认想要删除记录吗？', function(r) {
					if (r) {
						$.post('user/delete/' + userId,
								function(data) {
									if (data.success) {
										$('#user_dg').datagrid('reload');
										showMsg("操作成功。");
									} else {
										$.messager.alert('错误提示', data.message,
												'error');
									}
								});
					}
				});
			}

			function saveUser() {
				var form = $('#user_form').form('submit', {
					success : function(data) {
						data = $.parseJSON(data);
						if (data.success) {
							$('#user_dg').datagrid('reload');
							$('#user_win').window('close');
							showMsg("操作成功。");
						} else {
							$.messager.alert('错误提示', data.message, 'error');
						}
					}

				});

			}
			
			$(function() {
				init();
			});
		</script>
	</div>
</body>
</html>
